﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BenzInfo.Hockey.Grenadiers.Models;
using MvcEnumDropDownList.Infrastructure;
using System.Data.Objects;

namespace BenzInfo.Hockey.Grenadiers.Controllers
{
    public class QueriesController : Controller
    {
        private GrenadiersContext db = new GrenadiersContext();

        public JsonResult GetEvents(long start = 0, long end = 0)
        {
            string lang = (string)Session["Lang"] ?? "fr";

            return this.Json(from evnt in db.Events
                         select new
                         {
                             type = evnt is Game ? "Game" : "Event",
                             id = evnt.Id,
                             title = lang == "fr" ? evnt.TitleFR : evnt.TitleEN,
                             start = evnt.DateStart,
                             end = evnt.DateEnd,
                             color = evnt is Game ? (evnt as Game).HomeTeam.Name == "Grenadiers" ? "#700706" : "#000042" : "#d99d2f"
                         }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetNews()
        {
            string lang = (string)Session["Lang"] ?? "fr";

            return this.Json(from post in db.Posts
                             where post.Key == "News"
                             orderby post.DateCreated descending
                             select new
                             {
                                 Title = lang == "fr" ? post.TitleFR : post.TitleEN,
                                 Content = lang == "fr" ? post.ContentFR : post.ContentEN,
                                 Id = post.Id,
                                 Picture = post.Picture,
                                 Date = post.DateModified
                             }, JsonRequestBehavior.AllowGet);
        }
    }
}
